package com.kk.permission.huawei;

import android.view.accessibility.AccessibilityNodeInfo;

import com.kk.permission.accessibilityutils.NodeUtil;

import java.util.List;

public class HuaweiActionUtil {
    public static void scrollForward(AccessibilityNodeInfo accessibilityNodeInfo) {
        accessibilityNodeInfo = recycle(accessibilityNodeInfo);
        if (accessibilityNodeInfo != null) {
            accessibilityNodeInfo.performAction(4096);
        }
    }

    public static boolean pageContains(AccessibilityNodeInfo accessibilityNodeInfo, String str) {
        return NodeUtil.pageContains(accessibilityNodeInfo, str);
    }

    private static AccessibilityNodeInfo recycle(AccessibilityNodeInfo accessibilityNodeInfo) {
        if (accessibilityNodeInfo == null) {
            return null;
        }
        CharSequence className = accessibilityNodeInfo.getClassName();
        if (className == null) {
            return null;
        }
        String toLowerCase = className.toString().toLowerCase();
        if ((toLowerCase.contains("listview") || toLowerCase.contains("gridview") || toLowerCase.contains("scrollview") || toLowerCase.contains("recyclerview")) && accessibilityNodeInfo.isVisibleToUser() && !toLowerCase.contains("horizontalscrollview")) {
            return accessibilityNodeInfo;
        }
        if (accessibilityNodeInfo.getChildCount() == 0) {
            return null;
        }
        for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
            AccessibilityNodeInfo recycle = recycle(accessibilityNodeInfo.getChild(i));
            if (recycle != null) {
                return recycle;
            }
        }
        return null;
    }

    public static boolean performSwitch(AccessibilityNodeInfo accessibilityNodeInfo, String str) {
        List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(str);
        if (findAccessibilityNodeInfosByText == null || findAccessibilityNodeInfosByText.size() <= 0) {
            return false;
        }
        if (findAccessibilityNodeInfosByText.size() > 0) {
            AccessibilityNodeInfo parent = ((AccessibilityNodeInfo) findAccessibilityNodeInfosByText.get(0)).getParent();
            if (parent.isVisibleToUser()) {
                try {
                    accessibilityNodeInfo = getSwitch((AccessibilityNodeInfo) findAccessibilityNodeInfosByText.get(0));
                    if (/*!parent.performAction(16) && */!accessibilityNodeInfo.isChecked()) {
                        accessibilityNodeInfo.performAction(16);
                    }else
                        return false;
                } catch (Exception unused) {
                    return true;
                }
            }
        }
        return true;
    }

    public static AccessibilityNodeInfo getSwitch(AccessibilityNodeInfo accessibilityNodeInfo) {
        while (accessibilityNodeInfo != null) {
            AccessibilityNodeInfo switchCycle = getSwitchCycle(accessibilityNodeInfo);
            if (switchCycle != null) {
                return switchCycle;
            }
            accessibilityNodeInfo = accessibilityNodeInfo.getParent();
        }
        return null;
    }

    private static AccessibilityNodeInfo getSwitchCycle(AccessibilityNodeInfo accessibilityNodeInfo) {
        if (accessibilityNodeInfo != null && (accessibilityNodeInfo.getClassName().equals("android.widget.Switch") || accessibilityNodeInfo.getClassName().equals("android.widget.CheckBox"))) {
            return accessibilityNodeInfo;
        }
        if (accessibilityNodeInfo == null || accessibilityNodeInfo.getChildCount() == 0) {
            return null;
        }
        for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
            AccessibilityNodeInfo switchCycle = getSwitchCycle(accessibilityNodeInfo.getChild(i));
            if (switchCycle != null) {
                return switchCycle;
            }
        }
        return null;
    }

    public static void clickByTextAll(AccessibilityNodeInfo accessibilityNodeInfo, String str) {
        if (accessibilityNodeInfo != null) {
            List<AccessibilityNodeInfo> findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(str);
            if (findAccessibilityNodeInfosByText != null) {
                for (AccessibilityNodeInfo accessibilityNodeInfo2 : findAccessibilityNodeInfosByText) {
                    if (accessibilityNodeInfo2.isClickable()) {
                        accessibilityNodeInfo2.performAction(16);
                    }
                }
            }
        }
    }
}
